package com.tzxs.pay.main.gather.resource;

import android.app.Application;
import android.app.MediaRouteActionProvider;


import com.tzxs.pay.main.MapUtil;
import com.tzxs.pay.main.bill.detail.model.BankRefundBean;
import com.tzxs.pay.main.bill.detail.model.CancelAutBean;
import com.tzxs.pay.main.gather.model.BankBackBean;
import com.tzxs.pay.main.gather.model.BankBean;
import com.tzxs.pay.util.ISharedPreference;
import com.tzxs.pay.util.RetrofitUtil;
import com.tzxs.pay.util.RxUtil;
import com.tzxs.pay.util.netnavigator.NetNavigator;

import java.lang.ref.WeakReference;
import java.util.Map;

/**
 * 银行卡相关
 */
public class TradeResource {
    private static TradeResource INSTANCE;

    private WeakReference<Application> app;

    private ISharedPreference sharedPreference;

    private TradeResource(WeakReference<Application> app) {
        this.app = app;
        sharedPreference = ISharedPreference.getInstance(app.get());
    }

    public static TradeResource getInstance(Application application) {
        if (INSTANCE == null) {
            synchronized (TradeResource.class) {
                INSTANCE = new TradeResource(new WeakReference<>(application));
            }
        }
        return INSTANCE;
    }


    /**
     * 创建银行卡订单
     *
     * @param amount    金额
     * @param payType   类型 pay普通,auth预授权
     * @param remark    备注
     * @param authType  预授权结算时为1
     * @param orderId   订单号  结算时传入
     * @param navigator 回调
     */
    public void createBanOrder(double amount, String payType, String remark, int authType,
                               String orderId,NetNavigator<BankBean> navigator) {
        RxUtil.ioMain(RetrofitUtil.getInstance().apiService.creatBankOrder(sharedPreference.getToken()
                , amount, payType, remark, authType, orderId,MapUtil.getInstance().getMap()), navigator);
    }

    /**
     * 银行卡交易回调
     *
     * @param data      data
     * @param navigator
     */
    public void uploadTradeInfo(String data, NetNavigator<BankBackBean> navigator) {
        RxUtil.ioMain(RetrofitUtil.getInstance().apiService.uploadBankInfo(sharedPreference.getToken(), data), navigator);
    }

    /**
     * 微信   支付宝  预授权撤销
     *
     * @param auth_no   授权号
     * @param navigator
     */
    public void cancelAut(String auth_no, String password, String remark, NetNavigator<CancelAutBean> navigator) {
        RxUtil.ioMain(RetrofitUtil.getInstance().apiService.cancelAut(sharedPreference.getToken(), auth_no, password, remark), navigator);
    }

    /**
     * 银行卡预授权撤销
     *
     * @param orderId   订单号
     * @param navigator 回调
     */
    public void cancleBankAut(String orderId, NetNavigator<BankBean> navigator) {
        RxUtil.ioMain(RetrofitUtil.getInstance().apiService.cancleBankAut(sharedPreference.getToken(), orderId,MapUtil.getInstance().getMap()), navigator);
    }

    //银行卡退款订单创建
    public void bankRefundOrCreate(float amount, String password, String remark, String orderId,  NetNavigator<BankRefundBean> navigator) {
        RxUtil.ioMain(RetrofitUtil.getInstance().apiService.bankRefund(sharedPreference.getToken(), amount, password, remark, orderId,MapUtil.getInstance().getMap()), navigator);
    }
}
